﻿using System;
using System.Collections.Generic;

using System.Text;

namespace SEO.Utility
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 数据加密与解密
    /// 完成日期：2011年4月14日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class DataCode
    {
        /// <summary>
        /// 加密数据
        /// </summary>
        /// <param name="strEnValue">输入加密字符串</param>
        /// <param name="strEnKey">加密密锁(注意:长度至少8位)</param>
        /// <param name="iNum">加密循环次数</param>
        /// <returns>返回加密好的字符串</returns>
        public static string EnCode(string strEnValue, string strEnKey, int iNum)
        {
            for (int i = 0; i < iNum; i++)
            {
                DESEncryptor dese = new DESEncryptor();
                dese.InputString = strEnValue;//加密字段
                dese.EncryptKey = strEnKey;//加密密锁
                dese.DesEncrypt();
                string strMingWen = dese.OutString;
                strEnValue = strMingWen;
                dese = null;
            }

            return strEnValue;

        }
        /// <summary>
        /// 解密数据
        /// </summary>
        /// <param name="strDeValue">需要解密的字符串</param>
        /// <param name="strDeKey">解密密锁,必须与加密密锁保持一致</param>
        ///  <param name="iNum">解密循环次数,必须与加密循环次数相等</param>
        /// <returns>返回解密好的字符串</returns>
        public static string DeCode(string strDeValue, string strDeKey, int iNum)
        {
            for (int i = 0; i < iNum; i++)
            {
                DESEncryptor dese = new DESEncryptor();
                dese.InputString = strDeValue;//解密字段
                dese.DecryptKey = strDeKey;//解密密锁
                dese.DesDecrypt();
                string strMingWen = dese.OutString;
                strDeValue = strMingWen;
                if (strDeValue == null)
                {
                    System.Web.HttpContext.Current.Response.Redirect("/error.htm");
                }
                dese = null;
            }
            return strDeValue;
        }
    }
}
